package com.hmf.service;

import com.hmf.bean.Bicycle;
import com.hmf.bean.Role;
import com.hmf.bean.User;
import com.hmf.dao.HmfUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service//告诉spring对象，我要把这个类创建对象放入spring容器
public class HmfUserServiceImpl implements HmfUserService {
    @Autowired//ioc取创建的类，自动装配bean到UserServiceImpl的属性中，即相当于userMapper= new UserMapperImpl()
    HmfUserMapper hmfUserMapper;

    public User queryUserByName(String username) {
        User user = hmfUserMapper.queryUserByName(username);
        return user;

    }

    //注册
    public int register(User user) {
        // 查询用户是否已存在
        User rUser = hmfUserMapper.queryUserByName(user.getUsername());
        if (rUser != null && rUser.getUsername().equals(user.getUsername())) {
            System.out.println("User already exists.");
            return 1; // 用户已存在
        }// 用户不存在，执行注册操作
            int r = hmfUserMapper.register(user);
            System.out.println(r + "插入数据");
            if (r == 1) {
                System.out.println("Registration successful.");
                return 0; // 注册成功
            } else {
                System.out.println("Registration failed.");
                return 2; // 插入失败
            }
    }

    //登录
    @Override
    public List<User> login(Map<String,Object> map) {
        return hmfUserMapper.login(map);
    }

    //查询所有角色
    @Override
    public List<Role> showAllRole(Map<String,Object> params) {
        return hmfUserMapper.showAllRole(params);
    }

    //验证密码
    @Override
    public int verifyPassword(User user) {
        Map<String,Object> params = new HashMap<>();
        params.put("userID",user.getUserID());
        params.put("password",user.getPassword());
        return hmfUserMapper.verifyPassword(params);
    }

    //修改密码
    @Override
    public int updatePassword(User user) {
        Map<String,Object> params = new HashMap<>();
        params.put("userID",user.getUserID());
        params.put("password",user.getPassword());
        return hmfUserMapper.updatePassword(params);
    }

    //查看用户所有信息
    @Override
    public List<User> userMsg(){
        List<User> user=hmfUserMapper.userMsg();
        return user;
    }

    //查询
    @Override
    public User searchUser(String username){
        User user=hmfUserMapper.searchUser(username);
        if (user!=null){
            System.out.println("查询成功");
            return user;
        }else{
            System.out.println("查询失败");
            return null;
        }
    }

    //修改用户信息
    @Override
    public int updateMsg(User user){
        Map<String,Object> params = new HashMap<>();
        params.put("userID",user.getUserID());
        params.put("password",user.getPassword());
        params.put("email",user.getEmail());
        params.put("phoneNumber",user.getPhoneNumber());
        return hmfUserMapper.updateMsg(params);
    }
}
